import { Sprite } from '../engine/nodes/sprite.js';
import { vec3 } from '../engine/gl-matrix/index.js'
import { BoxCollider2D } from '../engine/core/boxCollider2D.js'
import { databus } from './databus.js'

export class Bullet extends Sprite {
    constructor(x, y, z) {
        super({ url: "images/bullet.png" });

        this.name = "bullet";
        this.scale = vec3.fromValues(0.062 * 0.6, 0.108 * 0.6, 1.0);
        this.translation = vec3.fromValues(x, y, z);
        this.material.renderOrder = 2;

        this.speed = databus.bulletSpeed;//每毫秒移动距离

        this.boxCollider = new BoxCollider2D({
            min: vec3.fromValues(-1, -1, 0),
            max: vec3.fromValues(1, 1, 0)
        });

        this.addEventListener("applyDamage", this.acceptDamage);
    }

    onUpdate(timestamp, frameDelta) {
        if (databus.gameOver || this.visible == false) return;

        this.translation[1] += frameDelta * this.speed;

        if (this.translation[1] > 1.2) {
            this.visible = false;
            this.emit("recover");
        }
    }

    reset(x, y, z) {
        vec3.set(this.translation, x, y, z);
        this.visible = true;
    }

    acceptDamage() {
        this.visible = false;
        this.emit("recover");
    }
}